<?php

namespace App\Console\Commands\Once;

use App\Kuafu\Log\Model\UserLoginLogModel;
use App\Kuafu\Order\Model\Clickhouse\OrderCHModel;
use App\Kuafu\Shop\Enum\CodeEnum;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\User\Model\UserFirstLoginModel;
use Illuminate\Console\Command;

class ExportShopTurnover extends Command
{
    protected $signature = 'once:export-shop-turnover';

    protected $description = '导出门店营业额';

    public function handle()
    {
        $shopList = Shop::query()
                        ->select('id', 'shopCode', 'name', 'status', 'appletStatus')
                        ->where('status', CodeEnum::SHOP_STATUS_NORMAL)
                        ->where('shopCode', '<', 9999)
                        ->where('appletStatus', \App\Kuafu\Shop\Enum\CodeEnum::SHOP_APPLET_STATUS_NORMAL)
                        ->get();
        $shopList = $shopList->keyBy('id');

        // 循环查询数据
        $startTimestamp = strtotime('2023-01-01');
        $endTimestamp   = strtotime('2024-01-01');
        $shopIdList     = $shopList->pluck('id')->toArray();
        // 获取堂食的营业数据
        $orderStatistic = OrderCHModel::query()
                                      ->select(raw('shopId,
                                           count(orderId) as countNum,
                                    sum(realPayPrice) as canteenOrderReceivedAmountSum,
                                    sum(originalPrice) as canteenOrderTurnoverSum'))
                                      ->whereBetween('completeTime', [$startTimestamp, $endTimestamp])
                                      ->whereIn('shopId', $shopIdList)
                                      ->groupBy('shopId')
                                      ->get()
                                      ->keyBy('shopId');


        // 获取堂食的营业数据
        $orderAppletStatistic = OrderCHModel::query()
                                            ->select(raw('shopId,
                                            count(orderId) as countNum,
                                    sum(realPayPrice) as canteenOrderReceivedAmountSum,
                                    sum(originalPrice) as canteenOrderTurnoverSum'))
                                            ->whereBetween('completeTime', [$startTimestamp, $endTimestamp])
                                            ->where('sourceType', \App\Kuafu\Order\Enum\CodeEnum::SOURCE_TYPE_MINI)
                                            ->whereIn('shopId', $shopIdList)
                                            ->groupBy('shopId')
                                            ->get()
                                            ->keyBy('shopId');

        $csvTitle = '门店编号,门店名称,堂食营业额,堂食实收金额,堂食订单量,小程序堂食营业额,小程序堂食实收金额,小程序订单量' . PHP_EOL;
        $row      = $csvTitle;
        foreach ($orderStatistic as $orderShop) {
            $row .= vsprintf('%s,%s,%s,%s,%s,%s,%s,%s' . PHP_EOL, [
                $shopList[$orderShop['shopId']]['shopCode'],
                $shopList[$orderShop['shopId']]['name'],
                bcdiv100($orderStatistic[$orderShop['shopId']]['canteenOrderTurnoverSum']),
                bcdiv100($orderStatistic[$orderShop['shopId']]['canteenOrderReceivedAmountSum']),
                ($orderStatistic[$orderShop['shopId']]['countNum']),
                bcdiv100($orderAppletStatistic[$orderShop['shopId']]['canteenOrderTurnoverSum'] ?? 0),
                bcdiv100($orderAppletStatistic[$orderShop['shopId']]['canteenOrderReceivedAmountSum'] ?? 0),
                ($orderAppletStatistic[$orderShop['shopId']]['countNum'] ?? 0)
            ]);

        }

        echo $row;
    }
}
